<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>JS实现</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            min-height: 100vh;
            font-family: Helvetica;
        }

        ul,
        li {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
        }

        .sliders {
            position: relative;
            width: 600px;
            height: 280px;
            overflow: hidden;
        }

        .slider {
            position: absolute;
            top: 0;
            left: 0;
            width: inherit;
            height: inherit;

            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 120px;
            color: #fff;
            transition: 0.5s transform ease-in-out;
        }

        .slider:nth-of-type(1) {
            background-color: lightblue;
            left: 0;
        }

        .slider:nth-of-type(2) {
            background-color: lightpink;
            left: 100%;
        }

        .slider:nth-of-type(3) {
            background-color: lightgray;
            left: 200%;
        }

        input[type="radio"] {
            display: none;
            position: relative;
            /* z-index: 100; */
        }

        .controls-visible {
            position: absolute;
            bottom: 12px;
            width: 100%;
            text-align: center;
        }

        .controls-visible label {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: #fff;
            margin: 0 3px;
            border: 2px solid #fff;
        }

        .sliders input[type="radio"]:nth-of-type(1):checked~.controls-visible label:nth-of-type(1) {
            background-color: #333;
        }

        .sliders input[type="radio"]:nth-of-type(2):checked~.controls-visible label:nth-of-type(2) {
            background-color: #333;
        }

        .sliders input[type="radio"]:nth-of-type(3):checked~.controls-visible label:nth-of-type(3) {
            background-color: #333;
        }

        .sliders input[type="radio"]:nth-of-type(1):checked~.slider {
            transform: translateX(0%);
        }

        .sliders input[type="radio"]:nth-of-type(2):checked~.slider {
            transform: translateX(-100%);
        }

        .sliders input[type="radio"]:nth-of-type(3):checked~.slider {
            transform: translateX(-200%);
        }
    </style>
</head>

<body>
    <ul class="sliders">
        <input type="radio" id="control-1" name="control" checked />
        <input type="radio" id="control-2" name="control" />
        <input type="radio" id="control-3" name="control" />
        <li class="slider">1</li>
        <li class="slider">2</li>
        <li class="slider">3</li>
        <div class="controls-visible">
            <label for="control-1"></label>
            <label for="control-2"></label>
            <label for="control-3"></label>
        </div>
    </ul>

    <script>
        const labelArr = document.querySelectorAll("label");
        const len = labelArr.length;
        let i = 0;
        let intervalTimer = null;

        const controls = document.querySelector(".controls-visible");
        const sliders = document.querySelector(".sliders");
        
        controls.addEventListener("click", function (e) {
            const str = String(e.target.getAttribute("for"));
            i = Number(str?.slice(str.length - 1));
            console.log("i", i);
        });

        sliders.addEventListener("mouseenter", function () {
            console.log("mouseenter");
            clearInterval(intervalTimer);
            intervalTimer = null;
        });

        sliders.addEventListener("mouseleave", function () {
            console.log("mouseleave");
            if (!intervalTimer) {
                switchSlider();
            }
        });

        const switchSlider = () => {
            intervalTimer = setInterval(() => {
                labelArr[i++]?.click();
                i === len && (i = 0);
            }, 2000);
        };

        switchSlider();
    </script>
</body>

</html>